Here's my code:
module.exports = {
name: 'suggestions',
aliases: ['suggest'],
execute(message, args, cmd, client, discord) {
let channel = message.guild.channel.cache.find(ch=>ch.id==='890313569260937247')
if(!channel) return message.channel.send({ content: 'There is no suggestion channel on this Server!' });
let messageArgs = args.join(' ')
const embed = new Discord.MessageEmbed()
.setColor('#008000')
.setAuthor(message.author.tag)
.setDescription(messageArgs)
.setTitle(`${message.author.tag} has a suggestion!!`)
message.channel.send({ embeds: [embed] })
}
}
I'm basically creating a command with my command Handler, doing a suggestion command, where the arguments of an user are going to be sent in a MessageEmbed.
If I run the actual command, this error shows up:
TypeError: Cannot read property 'channel' of undefined
at Object.execute (D:\Programming\Workspaces\Discord Bots\Mita Bot v1\commands\suggestions.js:5:37)
at module.exports (D:\Programming\Workspaces\Discord Bots\Mita Bot v1\events\guild\message.js:11:25)
at Client.emit (node:events:394:28)
at MessageCreateAction.handle (D:\Programming\Workspaces\Discord Bots\Mita Bot v1\node_modules\discord.js\src\client\actions\MessageCreate.js:31:18)
at Object.module.exports [as MESSAGE_CREATE] (D:\Programming\Workspaces\Discord Bots\Mita Bot v1\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (D:\Programming\Workspaces\Discord Bots\Mita Bot v1\node_modules\discord.js\src\client\websocket\WebSocketManager.js:345:31)
at WebSocketShard.onPacket (D:\Programming\Workspaces\Discord Bots\Mita Bot v1\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
at WebSocketShard.onMessage (D:\Programming\Workspaces\Discord Bots\Mita Bot v1\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10)
at WebSocket.onMessage (D:\Programming\Workspaces\Discord Bots\Mita Bot v1\node_modules\discord.js\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (node:events:394:28)
As t.niese said in their comment, Message#guild can be null. If message.guild === null, this means the message was sent in a DM.
Check if the message was sent in a guild first:
if (!message.guild)
return message.channel.send('This command can only be used in a server!')
const channel = message.guild.channels.get('890313569260937247')
if (!channel)
return message.channel.send('There is no suggestion channel on this Server!')
Some other points:
channel property on Guild; use channels instead.Collection#get to get a channel by its id (no need to use .find(c => c.id === someId).send: channel.send('the message') instead of channel.send({ content: 'the message' }).'channel' is not a property of the Guild class, you probably meant Guild#channels which returns the necessary type of GuildChannelManager.